# -*- coding: utf-8 -*-
import scrapy
from scrapy.selector import Selector
from data_acqr.items import DataAcqrItem


class ShenmezhidemaiSpider(scrapy.Spider):
    name = 'shenmezhidemai'
    allowed_domains = ['https://www.smzdm.com/']
    start_urls = ['https://search.jd.com/Search?keyword=气泡水&enc=utf-8&wq=气泡水&pvid=31e69910f9a0466aa859c0bbe5e10f55']

    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        content_list = Selector(response).xpath('//div[@id="J_goodsList"]/ul/li')
        for content in content_list:
            item = DataAcqrItem()
            item['product_link'] = content.xpath('./div/div/a/@href').extract_first()
            print(item['product_link'])
            # yield scrapy.Request(item['product_link'], meta={'item': item}, callback=self.parse_detail_content)

    def parse_detail_content(self, response):
        comment_list = Selector(response).xpath('//div[@class="tab_info"]/ul/li')
        for comment in comment_list:
            item = response.meta['item']
            item['comment'] = comment.xpath('./div[@class="comment_conBox"]/')
            comment_tm = comment.xpath('./div[@class="comment_conBox"]/div/div')
            comment_tm = comment_tm.xpath('./@content').extract_first() + comment_tm.xpath('./text()').extract_first()
            item['comment_tm'] = comment.xpath('./div[@class="comment_conBox"]/div/div/meta/')
